<template>
    <div class="box-content">
        <div class="navigation">日志管理/日志列表</div>
        <!--搜索-->
        <div class="mt-3">
            <div>
                <user-search class="w-25 mrt-10" @changeName="changeName"></user-search>
                <el-date-picker
                        class=" mrt-10"
                        style="width: 350px;"
                        v-model="searchParams.uploadDate"
                        type="datetimerange"
                        range-separator="~"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>
                <el-button type="primary" @click="onSearch">查询</el-button>
            </div>
        </div>
        <!--表格-->
        <div class="mt-3">
            <el-table
                    :data="logData"
                    v-loading="loading"
                    align="center"
                    :element-loading-text="loadText"
                    :header-cell-style="tableHeaderColor"
                    stripe
                    style="width: 100%">
                <el-table-column
                        prop="user.name"
                        label="操作用户"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="操作名称"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="ip"
                        label="操作ip"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="model"
                        label="模型"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        label="操作时间"
                        align="center"
                >
                    <template slot-scope="scope">
                        <div align="center">{{scope.row['created_at']?scope.row['created_at'].slice(0, 11):'无'}}</div>
                        <div align="center">{{scope.row['created_at']?scope.row['created_at'].slice(11 ):'无'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作"
                        fixed="right"
                        align="center"
                        width="100"
                >
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="mt-3 float-right">
                <el-pagination
                        background
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageInfo.current_page"
                        :page-size="pageInfo.per_page"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageInfo.total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
    import UserSearch from '../../components/UserInfo.vue'
    export default {
        name: "BoxList",
        components:{
            UserSearch
        },
        data() {
            return {
                searchParams: {
                    merchantName: '',
                    uploadDate: []
                },
                loading:false,
                loadText:'数据加载中',
                searchStr:'',
                pageInfo: {
                    per_page: 10,
                    total: 0,
                    current_page: 1,
                },
                logData: []
            }
        },
        created() {
            this.getLogList();
        },
        methods: {
            changeName(val) {
                this.searchParams.merchantName = val;
            },
            getLogList() {
                this.loading = true;
                this.$http.get('api/admin/user/actionlog' + this.searchStr,{
                    params: {
                        pageSize: this.pageInfo.per_page,
                        page: this.pageInfo.current_page
                    }
                })
                    .then((result)=>{
                        this.logData = result.data;
                        this.pageInfo.total = result.meta.total;
                        this.loading = false;
                    })
                    .catch((err) => {
                        this.loading = false;
                        this.$message.warning(err.data.msg || "数据异常，请重试");
                    })
            },
            onSearch() {
                this.searchStr = "";
                if (this.searchParams.merchantName) {
                    this.addConStr();
                    this.searchStr += `user_id=${this.searchParams.merchantName}`;
                }
                if (this.searchParams.uploadDate && this.searchParams.uploadDate.length > 0) {
                    this.addConStr();
                    this.searchStr+=`created_at$=>=${this.searchParams.uploadDate[0]},<=${this.searchParams.uploadDate[1]}`;
                }
                this.pageInfo.current_page = 1;
                this.getLogList();
            },
            addConStr() {
                if (this.searchStr.length > 0) {
                    this.searchStr += '&';
                } else {
                    this.searchStr += '?';
                }
            },
            handleDelete(val){
                this.$confirm('此操作将永久删除此记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete('api/admin/user/actionlog/' + val.id)
                        .then((result) => {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                during:1000
                            });
                            this.onSearch();
                        })
                        .catch((err) => {
                            this.$message.warning(err.data.msg || "数据异常，请重试");
                        })
                }).catch(() => {
                });
            },
            submitCostomer(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            handleSizeChange(val) {
                this.pageInfo.current_page = 1;
                this.pageInfo.per_page = val;
                this.getLogList();
            },
            handleCurrentChange(val) {
                this.pageInfo.current_page = val;
                this.getLogList();
            },
            tableHeaderColor({row, column, rowIndex, columnIndex}) {
                if (rowIndex === 0) {
                    return 'background-color: #FAFAFA;color: #000;font-weight: 500;height:54px;font-size:14px'
                }
            },
        }
    }
</script>

<style scoped>
</style>